from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
import Apps.homepage.RSSReader
from Apps.data.models import article

def index(request):
    pos=request.GET.get("pos")
    sport = article.objects.filter(category='sport');
    size=sport.count();
    sport_new =sport[size-5:size]
    sport_new.reverse();
    s1=sport_new[0]
    s2=sport_new[1]
    s3=sport_new[2]
    s4=sport_new[3]
    s5=sport_new[4]
    ngoaihang=sport.filter(typeOf="giai-ngoai-hang-anh");
    size = ngoaihang.count()
    ngoaihang=ngoaihang[size-3:size]
    ngoaihang=ngoaihang.reverse();
    ngoaihang_title=ngoaihang[0]
    ngoaihang=ngoaihang[1:3]
    euro=sport.filter(typeOf="euro_2012")
    size=euro.count();
    euro=euro[size-3:size]
    euro=euro.reverse();
    euro_title=euro[0];
    euro=euro[1:3]
    laliga=sport.filter(typeOf="la-liga")
    size=laliga.count();
    laliga=laliga[size-3:size]
    laliga=laliga.reverse()
    laliga_title=laliga[0]
    laliga=laliga[1:3]
    champ=sport.filter(typeOf="other")
    size=champ.count();
    champ=champ[size-3:size]
    champ_title=champ[0]
    champ=champ[1:3]
    champon=sport.filter(typeOf="champions-league")
    champon=champon[0]
    tech=article.objects.filter(category="technology")
    size=tech.count()
    other=tech[0:size-6]
    tech=tech[size-6:size]
    tech=tech.reverse();
    tech1=tech[0]
    tech2=tech[1]
    tech3=tech[2]
    tech4=tech[3]
    tech5=tech[4]
    tech6=tech[5]
    enter=article.objects.filter(category="entertainment")
    vanhoa=enter.filter(typeOf="vanhoa")
    amnhac=enter.filter(typeOf="amnhac")
    thoitrang=enter.filter(typeOf="thoitrang")
    orther=enter.filter(typeOf="other")
    size=orther.count()
    orther=orther[size-4:size]
    orther=orther.reverse();
    phim=enter.filter(typeOf="phim")
    size=vanhoa.count()
    en_new=enter[size-5:size]
    en_new=en_new.reverse()
    e1=en_new[0]
    e2=en_new[1]
    e3=en_new[2]
    e4=en_new[3]
    e5=en_new[4]
    vanhoa=vanhoa[size-3:size]
    vanhoa=vanhoa.reverse()
    vanhoa_title=vanhoa[0]
    vanhoa=vanhoa[1:3]
    size=thoitrang.count();
    thoitrang=thoitrang[size-3:size]
    thoitrang=thoitrang.reverse()
    thoitrang_title=thoitrang[0]
    thoitrang=thoitrang[1:3]
    size=amnhac.count()
    amnhac=amnhac[size-3:size]
    amnhac=amnhac.reverse()
    amnhac_title=vanhoa[0]
    amnhac=amnhac[1:3]
    size=phim.count()
    phim=phim[size-3:size]
    phim=phim.reverse()
    phim_title=phim[0]
    phim=phim[1:3]
    tonghop=article.objects.filter(category="total")
    size=tonghop.count();
    tong=tonghop[0:size-6]
    tonghop=tonghop[size-6:size]
    t1=tonghop[0]
    t2=tonghop[1]
    t3=tonghop[2]
    t4=tonghop[3]
    t5=tonghop[4]
    t6=tonghop[5]

    content={"ngoaihang":ngoaihang,"ngoaihang_title":ngoaihang_title,
             "euro":euro,"euro_title":euro_title,"laliga":laliga,"laliga_title":laliga_title,
             "champ":champ,"champ_title":champ_title,"champon":champon,
            "tech1":tech1,"tech2":tech2,"tech3":tech3,"tech4":tech4,"tech5":tech5,"tech6":tech6,"other":other,
            "vanhoa":vanhoa,"vanhoa_title":vanhoa_title,"thoitrang":thoitrang,"thoitrang_title":thoitrang_title,
            "amnhac":amnhac,"amnhac_title":amnhac_title,"phim":phim,"phim_title":phim_title,
            "s1":s1,"s2":s2,"s3":s3,"s4":s4,"s5":s5,"t1":t1,"t2":t2,"t3":t3,"t4":t4,"t5":t5,"t6":t6,"tong":tong,
            "pos":pos,"e1":e1,"e2":e2,"e3":e3,"e4":e4,"e5":e5,"orther":orther
            }
    return render_to_response('homepage/index.html',content, context_instance=RequestContext(request))

@login_required(login_url="/login/")
def profile(request):

    return render_to_response('registration/profile.html',  context_instance=RequestContext(request))


def rssRead(request):
    """
        Function: read RSS feed from other websites
    """
    rss_reader = Apps.homepage.RSSReader.RSSReader('http://vnexpress.net/rss/gl/the-thao.rss')
    rss_items = rss_reader.GetItems()
    return render_to_response('homepage/rssRead.html', {'rss_items': rss_items}, context_instance=RequestContext(request))

def getHTML(request, url):
    """
        Function: get the detail of a website
    """
    htmlString = Apps.homepage.RSSReader.getDetails(url)
    top = htmlString.index('<div class="main_content_detail"');
    if (top > 0):
        content1 = htmlString[top + 33: len(htmlString) - top - 34]
    bot = content1.rindex('class="share_bar w160 right"');
    content = content1[0:bot-5]

    content = content.replace('http://vnexpress.net','/detail?url=http://vnexpress.net')
    content = content.replace('"/gl/','/detail?url=http://vnexpress.net/gl/')
    content = content.replace('/Files', 'http://vnexpress.net/Files')
    content = content.replace('/Library', 'http://vnexpress.net/Library')
    content = content.replace('/Images', 'http://vnexpress.net/Images')
    content = content.replace('/Service', 'http://vnexpress.net/Service')
    return render_to_response('homepage/detailsRSS.html', {'htmlString': content}, context_instance=RequestContext(request))

